package com.lin.community_thumb_sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.community_thumb_sys.common.BusinessException;
import com.lin.community_thumb_sys.common.ErrorCode;
import com.lin.community_thumb_sys.common.ThrowUtils;
import com.lin.community_thumb_sys.common.UserConstant;
import com.lin.community_thumb_sys.model.domain.Blog;
import com.lin.community_thumb_sys.model.domain.Thumb;
import com.lin.community_thumb_sys.model.domain.User;
import com.lin.community_thumb_sys.model.dto.thumb.DoThumbRequest;
import com.lin.community_thumb_sys.service.BlogService;
import com.lin.community_thumb_sys.service.ThumbService;
import com.lin.community_thumb_sys.mapper.ThumbMapper;
import com.lin.community_thumb_sys.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

/**
* @author Lenovo
* @description 针对表【thumb】的数据库操作Service实现
* @createDate 2025-08-15 13:53:08
*/
@Service
public class ThumbServiceImpl extends ServiceImpl<ThumbMapper, Thumb> implements ThumbService{
    @Resource
    private TransactionTemplate transactionTemplate;
    @Resource
    private UserService userService;
    @Lazy
    @Resource
    private BlogService blogService;

    @Override
    public Boolean doThumb(DoThumbRequest doThumbRequest, HttpServletRequest request) {
        //判空
        ThrowUtils.throwIf(doThumbRequest == null || doThumbRequest.getBlogId() == null, ErrorCode.PARAMS_ERROR);
        //获取当前用户ID 博客ID
        User loginUser = userService.getLoginUser(request);
        Long blogId = doThumbRequest.getBlogId();

        //判断是否已经点赞 没点赞则更新
        synchronized (loginUser.getId().toString().intern()){
             return transactionTemplate.execute(status -> {
                boolean exists = this.lambdaQuery()
                        .eq(Thumb::getBlogId, blogId)
                        .eq(Thumb::getUserId, loginUser.getId())
                        .exists();
                //不为空则表示已点赞
                if(exists){
                    throw new BusinessException(ErrorCode.OPERATION_ERROR,"用户已点赞");
                }
                //同时更新博客表和点赞表
                Thumb thumb = new Thumb();
                thumb.setBlogId(blogId);
                thumb.setUserId(loginUser.getId());
                boolean save = this.save(thumb);

                //更新博客表 点赞次数加一
                boolean update = blogService.lambdaUpdate()
                        .eq(Blog::getId, blogId)
                        .setSql("thumbCount = thumbCount + 1")
                        .update();

                 if (!save || !update) {
                     status.setRollbackOnly();
                     throw new BusinessException(ErrorCode.OPERATION_ERROR, "更新失败");
                 }
                return true;
            });
        }
    }
}




